package kuang.juc.assist3;

import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;

//线程加法计数器
public class CyclicBarrierDemo {
    public static void main(String[] args) {
        //第二个参数为计数器达到值后执行的线程
        CyclicBarrier cyclicBarrier = new CyclicBarrier(7,()->{
            System.out.println("召唤神龙");
        });

        for (int i = 1; i <= 7; i++) {
            int finalI=i;
            new Thread(()->{
                System.out.println(Thread.currentThread().getName()+"收集到了第"+finalI+"个龙珠");
                try {
                    //等待计数器达到值
                    cyclicBarrier.await();
                } catch (InterruptedException | BrokenBarrierException e) {
                    e.printStackTrace();
                }
            }).start();
        }
    }
}
